package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.NewsClassIficationDaoImpl;
import com.ysd.Dao.Impl.UserDaoImpl;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminMessageServlet extends BaseServlet {
	/**
	 * 按状态分页查询留言全部留言 localhost:8080/zhubao/adminMessage?reqName=getMessage
	 * 
	 * @author 86176
	 *
	 */
	public void getMessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应编码格式
		PrintWriter out = response.getWriter();

		String status = request.getParameter("status");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		UserDaoImpl uDao = new UserDaoImpl();// 创建实现类对象
		List<User> uList = uDao.getMessageByStatus(status, (page - 1) * size, size);// 查询所有产品分类集合

		Gson gson = new Gson();
		String res = gson.toJson(uList);// 将uList转化为json字符串

		out.print(res);// 响应数据

		out.flush();
		out.close();
	}

	/**
	 * 查询集合长度 localhost:8080/zhubao/adminMessage?reqName=getAllMessage
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllMessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String status = request.getParameter("status");
		UserDaoImpl uDao = new UserDaoImpl();
		List<User> list = uDao.getAllMessage(status);

		Gson gson = new Gson();
		String res = gson.toJson(list.size());
		out.print(res);

		out.flush();
		out.close();
	}

	/**
	 * 根据留言id查询留言 数据回显 localhost:8080/zhubao/adminMessage?reqName=getMessageById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		int mId = Integer.parseInt(request.getParameter("mId"));
		System.out.println(mId);
		UserDaoImpl uDao = new UserDaoImpl();
		User user = uDao.getMessageById(mId);

		Gson gson = new Gson();
		String res = gson.toJson(user);
		out.print(res);
		System.out.println(res);

		out.flush();
		out.close();
	}

	/**
	 * 根据留言id修该留言状态 localhost:8080/zhubao/adminMessage?reqName=updateMessageById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		UserDaoImpl uDao = new UserDaoImpl();
		int mId = Integer.parseInt(request.getParameter("mId"));

		User user = uDao.getMessageById(mId);
		if (user.getStatus().equals("已读") || user.getStatus().equals("已回复")) {
			out.print(Result.toClient(true, "不需要修该", ""));
		} else {
			user = new User(mId, "已读", null);
			int n = uDao.updateMessageById(user);

			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * 根据留言id回复留言 localhost:8080/zhubao/adminMessage?reqName=updateMessageReplyById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageReplyById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		UserDaoImpl uDao = new UserDaoImpl();
		int mId = Integer.parseInt(request.getParameter("mId"));
		String reply = request.getParameter("reply");
		System.out.println(reply);
		User user = new User(mId, null, reply);
		int n = uDao.updateMessageReplyById(user);

		if (n > 0) {
			out.print(Result.toClient(true, "回复成功", ""));
		} else {
			out.print(Result.toClient(false, "回复失败", ""));
		}

		out.flush();
		out.close();
	}

	/**
	 * 删除指定留言 localhost:8080/zhubao/adminMessage?reqName=deleteMessage
	 * 
	 * @author 86176
	 *
	 */
	public void deleteMessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		int mId = Integer.parseInt(request.getParameter("messageId"));
		UserDaoImpl uDao = new UserDaoImpl();// 创建实现类对象
		int n = uDao.deleteMessageById(mId);// 调用删除方法

		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}
	/**
	 * 删除指定留言多条
	 *  localhost:8080/zhubao/adminMessage?reqName=deleteMessageByIds
	 * 
	 * @author 86176
	 *
	 */
	public void deleteMessageByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();// 创建实现类对象
		
		String ids = request.getParameter("messageId");
		int n = uDao.deleteMessageByIds(ids);

		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}
}
